<?php
declare(strict_types=1);

namespace Service\CommodityService;

use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\GoodsImportLog;

/**
 * 渠道商品excel导入日志服务
 * @author huj
 */
class ChannelCommodityImportLogService extends BaseService
{
    /**
     * excel导入日志列表
     * @param array $param
     */
    public function importLogList(array $param)
    {
        if (isset($param['page']) && ctype_digit((string)$param['page']) && $param['page'] > 0) {
            $this->page = $param['page'];
        }
        if (isset($param['limit']) && ctype_digit((string)$param['limit']) && $param['limit'] > 0) {
            $this->limit = $param['limit'];
        }
        if (!isset($param['cid']) || empty($param['cid']) || (preg_match("/^[1-9]\d*$/", (string)$param['cid']) != 1)) {
            return Normal::returnCode(Normal::FAIL, 'invalid argument cid');
        }
        $model = GoodsImportLog::where('cid', $param['cid']);
        if (isset($param['status']) && in_array($param['status'], ['1', '2'])) {
            $model = $model->where('status', $param['status']);
        }
        if (isset($param['import_type']) && in_array($param['import_type'], ['1', '2'])) {
            $model = $model->where('import_type', $param['import_type']);
        }
        if (isset($param['manager_id']) && ctype_digit((string)$param['manager_id']) && $param['manager_id'] > 0) {
            $model = $model->where('manager_id', $param['manager_id']);
        }
        if (isset($param['start_at']) || isset($param['end_at'])) {
            if (isset($param['start_at'])) {
                $startTsp = strtotime($param['start_at']);
                if ($startTsp) {
                    $model = $model->where('cd', '>=', $startTsp);
                }
            }
            if (isset($param['end_at'])) {
                $endTsp = strtotime($param['end_at']);
                if ($endTsp) {
                    $endTsp += 86399;
                    $model  = $model->where('cd', '<=', $endTsp);
                }
            }
        } else {
            //默认今天
            $model = $model->where('cd', '>=', strtotime(date('Y-m-d 00:00:00')))->where('cd', '<=', strtotime(date('Y-m-d 23:59:59')));
        }
        $fields = [
            'id', 'file_name', 'import_type', 'all_num', 'success_num', 'fail_num', 'status', 'origin_file_url', 'fail_file_url',
            'manager_id', 'manager_name', 'cd',
        ];
        $result = $model->select($fields)->orderBy('id', 'desc')->paginate($this->limit, ['*'], 'page', $this->page);
        if ($result->isEmpty()) {
            return Normal::returnCode(Normal::SUC, '获取成功', [
                'list'  => [],
                'total' => 0,
            ]);
        }
        $result->each(function ($item, $key) {
            $item->import_type_text = $item->import_type == 1 ? '修改商品资料' : '修改商品价格';
            $item->status_text      = $item->status == 1 ? '导入成功' : '导入失败';
            $item->cd_text          = date('Y-m-d H:i:s', $item->cd);
            return $item;
        });
        return Normal::returnCode(Normal::SUC, '获取成功', [
            'list'  => $result->items(),
            'total' => $result->total(),
        ]);
    }
}